package weaver.ihg.schedule;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang.time.DateUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 3.5企微同步人员信息版本表 定时任务
 */
public class ContactVersionForViewCron extends BaseCronJob {
    private  int howDay;//留存距离最近时间多久的日志

    public int getHowDay() {
        return howDay;
    }

    public void setHowDay(int howDay) {
        this.howDay = howDay;
    }

    @Override
    public void execute() {
        BaseBean baseBean=new BaseBean();
        baseBean.writeLog("ContactVersionForViewCron=====>howDay==>"+howDay);
        String sql="select id,job,email,phone,department,chinese_name,english_name," +
                "hotel_code,parent_department,merlin_id,accounttype,belongto,brand," +
                "hotel_name,hotel_name_zh,departmentID,name,status,requestId,userid from view_hrinfo where job in(select  tpdyqwgw from uf_hotelpositions where gwlb='1')";
        baseBean.writeLog("ContactVersionForViewCron=====>sql===>"+sql);
        RecordSet recordSet=new RecordSet();
        RecordSet recordSet2=new RecordSet();
        recordSet.execute(sql);
        boolean findAddData=false;
        String kuSql="select id,Update_date from Wecoms_Employees_version order by Update_date desc limit 0,1 where job in(select  tpdyqwgw from uf_hotelpositions where gwlb='1')";
        while (recordSet.next()){

            //Wecoms_Employees_version 企微同步人员信息版本表
//            字段名	字段类型	字段用处
//            id	integer	数据id
//            Wxid	char	企微id
//            inncode	char	酒店代码
//            name	char	人员姓名
//            email	char	邮箱
//            Wecom_alias	char	企微岗位
//            Hotel_role	char	TP酒店岗位
//            Update_date	char	更新日期
//            version	char	版本号
            //取一个配置留存多久日期天数的，
            baseBean.writeLog("kuSql===>"+kuSql);
            recordSet2.execute(kuSql);
            Date nowDate=null;
            if(recordSet2.next()){
               String  nowDateStr=recordSet2.getString("Update_date");
                baseBean.writeLog("nowDateStr=====>"+nowDateStr);
                nowDate=DateUtil.parse(nowDateStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                baseBean.writeLog("nowDate=====>"+nowDate);
                findAddData=true;
                Date date = DateUtils.addDays(nowDate, -howDay);
                String format = DateUtil.format(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                baseBean.writeLog("date====>"+date+"====>format===>"+format);
                String judgeSql="delete from Wecoms_Employees_version where Update_date <'"+format+"'";
                recordSet.execute(judgeSql);

            }
            String insertBuffer = "insert into Wecoms_Employees_version (wxid,jddm,name,email,Wecom_alias,Hotel_role,Update_date,version) " +
                    "values (?,?,?,?,?,?,?,?,?)";
            recordSet.executeUpdate(insertBuffer,recordSet.getString("userid"),recordSet.getString("hotel_code"),
                    recordSet.getString("name"), recordSet.getString("email"),recordSet.getString("job"),"", DateUtil.now(),""
            );
        }
        if(!findAddData){
            baseBean.writeLog("no find Add Data,不做重新排序");
            return;
        }
        //重新排序一下
        RecordSet recopyRecordSet=new RecordSet();
        recopyRecordSet.execute("select * from view_hrinfo where job in(select  tpdyqwgw from uf_hotelpositions where gwlb='1')");
        int i=1;
        while (recopyRecordSet.next()){
            recopyRecordSet.executeUpdate("update Wecoms_Employees_version set version='?' and id='?'",i+"",recopyRecordSet.getString("id"));
            i+=1;
        }
    }
}
